package com.excesys.exsecs.om.job;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.excesys.exsecs.om.entity.OmICheckSheet;
import com.excesys.exsecs.om.mapper.OmICheckSheetMapper;
import com.excesys.exsecs.om.service.OmICheckSheetService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class SheetToTimeOutJob {

	private final OmICheckSheetMapper omICheckSheetMapper;

	private final OmICheckSheetService omICheckSheetService;

	// @Scheduled(cron = "0/1 * * * * ?")
	public void createSheetJob() throws ParseException {
		log.info("开始处理逾期巡检单。。。。");
		List<OmICheckSheet> list = omICheckSheetMapper.getOverDueList();
		List<OmICheckSheet> sheetList = new ArrayList<>();
		DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		for (OmICheckSheet om : list) {
			if (StringUtil.isNotEmpty(om.getExcuteEndTime())) {
				boolean flag = timeCalendar(
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())),
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.parse(fmt.format(om.getCreateTime()) + " " + om.getExcuteEndTime() + ":00"));
				if (flag)
					sheetList.add(om);
			}
		}
		log.info("sheetList:", sheetList);
		for (OmICheckSheet omICheckSheet : sheetList) {
			omICheckSheet.setSheetStatus("5");
			omICheckSheetService.saveOrUpdate(omICheckSheet);
		}
		log.info("结束处理逾期巡检单。。。。");
	}

	public static boolean timeCalendar(Date nowTime, Date endTime) {
		// 设置当前时间
		Long startTime = nowTime.getTime();
		// 设置结束时间
		Long endTimes = endTime.getTime();
		// 处于开始时间之后，和结束时间之前的判断
		if (startTime.compareTo(endTimes) > 0) {
			return true;
		}
		else {
			return false;
		}
	}

}
